<?php


namespace App\Http\Controllers\Backend;

use App\Exceptions\CompanyApiException;
use App\Http\Requests\Company\CompanyProcessRequest;
use App\Services\Business\Models\CompanyProcess;

use App\Zesow\WechatMini;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Exceptions\ServiceException;

class CompanyProcessController extends BaseController
{


    public function index()
    {


        $companyId = $this->loginCompanyId();

        $process = CompanyProcess::query()
            ->when($companyId, function ($query) use ($companyId) {
                $query->where('company_id', $companyId);
            })
            ->orderBy('sort','asc')->get();

        return $this->successData(compact('process'));

    }

    public function store(CompanyProcessRequest $request, CompanyProcess $process)
    {

        $data = $request->filldata();
        $process->fill($data)->save();
        return $this->success(__('新增流程成功'));
    }


    /**
     * 流程编辑
     * @param $id
     * @return JsonResponse
     */
    public function edit($id)
    {
        $process = CompanyProcess::query()->where('id', $id)->firstOrFail();
        return $this->successData($process);
    }

    /**
     * 流程更新
     * @param CompanyProcessRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function update(CompanyProcessRequest $request, $id)
    {
        $process = CompanyProcess::query()->where('id', $id)->firstOrFail();
        $process->fill($request->filldata())->save();
        return $this->successData(compact('process'));
    }


    /**
     * 删除流程
     * @param $id
     * @return JsonResponse
     */
    public function delete($id)
    {
        $process = CompanyProcess::query()->where('id', $id)->first();

        if ($process->decorates()->exists()) {
            return $this->error(__('流程已有客户使用不支持删除'));
        }
        $process->delete();

        return $this->success(__('流程删除成功'));
    }


    /**
     *
     * 工艺流程排序
     * @param Request $request
     * @return JsonResponse
     */
    public  function  sort(Request $request)
    {
        $records = $request->input('processes_ids', []);
        $table = $request->input('process_table');
        DB::beginTransaction();
        try {
            $processString ='';
            foreach ($records as $k => $n) {
                $processId = $n['id'];
                $processSort = $n['sort'];
                $processIds[] = $processId;
                $processString .= "WHEN {$processId} THEN {$processSort} ";
                $updateVideoQuery = "UPDATE `{$table}` SET sort = CASE id {$processString} END WHERE id IN (" . implode(',', $processIds) . ")";
                DB::statement($updateVideoQuery);
            }

            // 提交事务
            DB::commit();
            return $this->successData(__('流程排序成功'));
        } catch (CompanyApiException $e) {
            // 回滚事务
            DB::rollBack();

            return $this->successData(__('流程排序失败'));
        }
    }










}
